<?php
use \phiction\tester as t;
use \phiction\query_builder as q;
use \phiction\schema_loader as s;

return [
    'insert' => function () {
        $ld = new s();
        $pdo = new \PDO("pgsql:");
        $q = new q($ld, $pdo);
        $plan = $q->load('comment')
            ->table("comment")
            ->field('author', 'body', 'post')
            ->insert(2, '3', 2)
            ->insert(2, 'yes', 1)
            ->insert(2, 'or', 3)
            ->plan();
        echo $plan['plan'];
        var_export($plan['values']);
    },
    'run-insert' => function () {
        $ld = new s();
        $pdo = new \PDO("pgsql:dbname=postgres");
        $q = new q($ld, $pdo);
        $result = $q->load('comment')
            ->table("comment")
            ->field('author', 'body', 'post')
            ->insert(2, '3', 2)
            ->insert(2, 'yes', 1)
            ->insert(2, 'or', 3)
            ->run();
        var_export($result);
    },
    'update' => function () {
        $ld = new s();
        $pdo = new \PDO("pgsql:");
        $q = new q($ld, $pdo);
        $plan = $q->load('comment')
            ->table("comment", 2)
            ->field('body')
            ->where(['author.login', 'hello'])
            ->where(['post', 1])
            ->update('it works')
            ->plan();
        echo $plan['plan'];
        var_export($plan['values']);
    },
    'run-update' => function () {
        $ld = new s();
        $pdo = new \PDO("pgsql:dbname=postgres");
        $q = new q($ld, $pdo);
        $result = $q->load('comment')
            ->table("comment", 2)
            ->field('body')
            ->where(['author.login', 'hello'])
            ->where(['post', 1])
            ->update('it works')
            ->run();
        var_export($result);
    },
    'select' => function () {
        $ld = new s();
        $pdo = new \PDO("pgsql:");
        $q = new q($ld, $pdo);
        $plan = $q->load('comment')
            ->table("comment", 3)
            ->where(['author.login', 'hello'], ['author', 10, '<'])
            ->field('author.login who', 'post.title')
            ->order('>post.author', 'id', '<post.author.login')
            ->select(0)
            ->plan();
        echo $plan['plan'];
        var_export($plan['values']);
    },
    'run-select' => function () {
        $ld = new s();
        $pdo = new \PDO("pgsql:dbname=postgres");
        $q = new q($ld, $pdo);
        $result = $q->load('comment')
            ->table("comment", 1)
            ->field('post.author.login reply-to', 'author.login author', 'post.body body-to', 'body', 'post.title')
            ->select(0)
            ->run();
        var_export($result);
    },
    'delete' => function () {
        $ld = new s();
        $pdo = new \PDO("pgsql:");
        $q = new q($ld, $pdo);
        $plan = $q->load('comment')
            ->table("comment")
            ->where(['id', 10, '>'])
            ->where(['author.login', 'hello'])
            ->delete()
            ->plan();
        echo $plan['plan'];
        var_export($plan['values']);
    },
    'run-delete' => function () {
        $ld = new s();
        $pdo = new \PDO("pgsql:dbname=postgres");
        $q = new q($ld, $pdo);
        $result = $q->load('comment')
            ->table("comment")
            ->where(['id', 10, '>'])
            ->where(['author.login', 'hello'])
            ->delete()
            ->run();
        var_export($result);
    },
];

